<template>
	<view class="RankingVue">
		<view class="RankingVue-list" v-for="(it,index) in level">
			<view class="flex-center RankingVue-list-c">
				<image src="/static/img/shi.png" mode=""></image>
				<view class="RankingVue-list-tx flex-center">
					<view> {{numberToChineseArray(index + 1)}}世</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props:{
			tree:{
				type: Array,
				default: () => []
			},
			type: {
				type: Number,
				default: () => 0 //0编辑 1 查看
			}
		},
		watch:{
			tree:{
				 immediate: true,
				handler(val){
					console.log('val', val)
					this.setLevel(val,1)
					console.log('val', val)
				},
				deep: true
			}
		},
		data(){
			return {
				level: 0
			}
		},
		methods:{
			setLevel(arr,level = 0) {
				if(Array.isArray(arr) && arr.length > 0) {
					arr.forEach(i=>{
						if(level >= this.level){
							this.level = level
						}
						i.level = level
						this.setLevel(i.child, level + 1)
					})
					
				}
			},
			getName(level){
				if(level < 10) {
					return [['一','二','三','四','五','六','七','八','九'][level]]
				} else if(level > 10 && level < 20) {
					if(level === 10) {
						return ['一','十']
					}
					return ['一','十',['一','二','三','四','五','六','七','八','九'][level - 10]]
				}
				
			},
			numberToChineseArray(num) {
			    // if (num <= 0 || !Number.isInteger(num)) {
			    //     throw new Error('请输入一个正整数');
			    // }
			
			    const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
			    const units = ['', '十', '百', '千', '万', '十', '百', '千', '亿'];
			
			    let strNum = num.toString();
			    let result = [];
			    let unitIndex = strNum.length - 1;
			
			    for (let i = 0; i < strNum.length; i++) {
			        let digit = parseInt(strNum[i], 10);
			        if (digit !== 0 || (digit === 0 &&i === strNum.length - 1)) {
			            result.push(chineseNums[digit]);
			            if (unitIndex > 0) {
			                result.push(units[unitIndex]);
			            }
			        }
			        unitIndex--;
			    }
			
			    // 移除多余的'零'
			    result = result.filter((value, index, self) => {
			        return value !== '零' || self.indexOf(value) === index;
			    });
			
			    return result;
			}
			
		}
	}
</script>

<style lang="scss">
	.RankingVue {
		width: 100%;
		
		.RankingVue-list {
			width: 100%;
			height: 238rpx;
			margin-bottom: 127rpx;
		
			.RankingVue-list-c {
				width: 100%;
				height: 100%;
				position: relative;
				image {
					width: 43rpx;
					height: 134rpx;
				}
				.RankingVue-list-tx {
					width: 100%;
					height: 100%;
					position: absolute;
					top: 0;
					left: 0;
					flex-direction: column;
					view {
						font-family: FZXuanZhenZhuanBianS-R-GB;
						font-weight: bold;
						font-size: 22rpx;
						color: #F6F1E7;
						transform: scale(0.8);
						width: 22rpx;
					}
				}
			}
		}
	}
</style>